//---------------------------------------------------------------------------
// Author : Nguyen Duc Khanh
// Created Date : Saturday, December 15, 2007
//---------------------------------------------------------------------------


using System;
using System.Data;
using System.Data.SqlClient;
using vs.Core;

namespace Forum
{
    public class CategoryController
    {
        #region Constructor
        public CategoryController() { }
        #endregion

        #region Function
        private const string INSERT_FORUM_CATEGORY = "INSERT INTO FORUM_Category (CategoryID, CategoryName, Description, ParentID, OrderIndex) "
         + " VALUES (@CategoryID, @CategoryName, @Description, @ParentID, @OrderIndex)";
        public int Insert(CategoryInfo cat)
        {
            int result = 0;
            try
            {
                DataParameter[] param = new DataParameter[5];
                param[0] = new DataParameter("@CategoryID", cat.CategoryID);
                param[1] = new DataParameter("@CategoryName", cat.CategoryName);
                param[2] = new DataParameter("@Description", cat.Description);
                param[3] = new DataParameter("@ParentID", cat.ParentID);
                param[4] = new DataParameter("@OrderIndex", cat.OrderIndex);
                result = DataFactory.Execute(INSERT_FORUM_CATEGORY, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string UPDATE_FORUM_CATEGORY = "UPDATE FORUM_Category SET "
             + " CategoryName = @CategoryName, "
             + " Description = @Description, "
             + " ParentID = @ParentID, "
             + " OrderIndex = @OrderIndex "
             + " WHERE (CategoryID = @CategoryID)";
        public int Update(CategoryInfo cat)
        {
            int result = 0;
            try
            {
                DataParameter[] param = new DataParameter[5];
                param[0] = new DataParameter("@CategoryName", cat.CategoryName);
                param[1] = new DataParameter("@Description", cat.Description);
                param[2] = new DataParameter("@ParentID", cat.ParentID);
                param[3] = new DataParameter("@OrderIndex", cat.OrderIndex);
                param[4] = new DataParameter("@CategoryID", cat.CategoryID);
                result = DataFactory.Execute(UPDATE_FORUM_CATEGORY, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string DELETE_FORUM_CATEGORY = "DELETE FROM FORUM_Category WHERE (CategoryID = @CategoryID)";
        public int Delete(string CategoryID)
        {
            int result = 0;
            try
            {
                DataParameter param = new DataParameter("@CategoryID", CategoryID);
                result = DataFactory.Execute(DELETE_FORUM_CATEGORY, param);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public CategoryInfo Converting(DataRow drFORUM_Category)
        {
            CategoryInfo result = null;
            try
            {
                result = new CategoryInfo();
                result.CategoryID = (string)drFORUM_Category["CategoryID"];
                result.CategoryName = (string)drFORUM_Category["CategoryName"];
                result.Description = (string)drFORUM_Category["Description"];
                result.ParentID = (string)drFORUM_Category["ParentID"];
                result.OrderIndex = (int)drFORUM_Category["OrderIndex"];
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string LOAD_FORUM_CATEGORY = "SELECT * FROM FORUM_Category WHERE (CategoryID = @CategoryID)";
        public CategoryInfo Load(string CategoryID)
        {
            CategoryInfo result = null;
            try
            {
                DataParameter[] param = new DataParameter[1];
                param[0] = new DataParameter("@CategoryID", CategoryID);
                DataTable dt = DataFactory.SelectTable(LOAD_FORUM_CATEGORY, param);
                if (dt.Rows.Count > 0) result = Converting(dt.Rows[0]);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        private const string LOAD_LIST_FORUM_CATEGORY = "SELECT * FROM FORUM_Category";
        public DataTable LoadTable()
        {
            DataTable result = null;
            try
            {
                result = DataFactory.SelectTable(LOAD_LIST_FORUM_CATEGORY);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        #endregion
    }
}
